package com.dao.imp;

import com.dao.AdminDao;
import com.entity.Company;
import com.entity.User;
import com.utils.DButil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.SQLException;

public class AdminDaoimpl implements AdminDao {
    @Override
    public int delCompany(String companyId) {
        Connection conn = DButil.getConn();
        String sql="DELETE from r_companyinfo where companyid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,companyId);
            int i = ps.executeUpdate();
            if (i==1){
                String sql1="DELETE from r_company where companyid=?";
                ps= conn.prepareStatement(sql1);
                ps.setString(1,companyId);
                int i1 = ps.executeUpdate();
                if(i1==1){
                    return  1;
                }else {
                    return 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;

    }

    @Override
    public int delUser(String userId) {
        Connection conn = DButil.getConn();
        String sql="DELETE from r_userinfo where userid=?";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,userId);
            int i = ps.executeUpdate();
            if (i==1){
                String sql1="DELETE from r_user where userid=?";
                ps= conn.prepareStatement(sql1);
                ps.setString(1,userId);
                int i1 = ps.executeUpdate();
                if(i1==1){
                    return  1;
                }else {
                    return 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int AddCompany(Company company) {
        Connection conn = DButil.getConn();
        String sql="insert  into r_companyinfo(companyid,companyname,companyaddress,companytel,companyemail,addtime,gongshangnum,state) values(?,?,?,?,?,?,?,?) ";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,company.getCompanyId());
            ps.setString(2,company.getCompanyName());
            ps.setString(3,company.getCompanyAddress());
            ps.setString(4,company.getCompanyTel());
            ps.setString(5,company.getCompanyEmail());
            ps.setString(6,company.getAddTime());
            ps.setString(7,company.getGongNum());
            ps.setString(8,company.getState());
            int i = ps.executeUpdate();
            if (i==1){
                String sql1="insert  into r_company(name,pwd,companyid,state) values(?,?,?,?) ";
                ps= conn.prepareStatement(sql1);
                ps.setString(1,company.getCompanyName());
                ps.setString(2,company.getPwd());
                ps.setString(3,company.getCompanyId());
                ps.setString(4,company.getState());
                int i1 = ps.executeUpdate();
                if(i1==1){
                    return  1;
                }else {
                    return 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;
    }

    @Override
    public int AddUser(User user) {
        Connection conn = DButil.getConn();
        String sql="insert  into r_userinfo(userid,uname,realname,usex,age,school,xueli,cardnum,zhuanye,photo,tel,state) values(?,?,?,?,?,?,?,?,?,?,?,?) ";
        try {
            PreparedStatement ps= conn.prepareStatement(sql);
            ps.setString(1,user.getUserId());
            ps.setString(2,user.getName());
            ps.setString(3,user.getRealName());
            ps.setString(4,user.getuSex());
            ps.setString(5,user.getAge());
            ps.setString(6,user.getSchool());
            ps.setString(7,user.getXueLi());
            ps.setString(8,user.getCardNum());
            ps.setString(9,user.getMajor());
            ps.setString(10,user.getPhoto());
            ps.setString(11,user.getTel());
            ps.setString(12,user.getState());
            int i = ps.executeUpdate();
            if (i==1){
                String sql1="insert  into r_user(name,pwd,userid,state) values(?,?,?,?) ";
                ps= conn.prepareStatement(sql1);
                ps.setString(1,user.getName());
                ps.setString(2,user.getPwd());
                ps.setString(3,user.getUserId());
                ps.setString(4,user.getState());
                int i1 = ps.executeUpdate();
                if(i1==1){
                    return  1;
                }else {
                    return 0;
                }
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }
        return 0;


    }
}
